package engine;





import java.util.ArrayList;

import objects.Ship;

public class Combat {
	

	Ship bad;
	Ship bad2;
	Ship player;
	
	int scanHull;
	int currentAction;
	
	boolean combatActive;
	//ArrayList<Ship> shipList;
	ArrayList<Ship> intQueue;
	
	
	//Empty debugging constructor
	public Combat(){
		
		bad = new Ship("Rawr", 5, 1);
		bad2 = new Ship("Double Rawr", 5, 1);
		
		
		bad.setCurrentInitiative(3);
		bad2.setCurrentInitiative(4);
		Game.getPlayer().getShip().setCurrentInitiative(1);
		player = Game.getPlayer().getShip();
		
		intQueue = new ArrayList<Ship>();
		intQueue.add(bad);
		intQueue.add(bad2);
		intQueue.add(player);
				
		
	}// empty constructor
	
	
	public Combat(ArrayList<Ship> a){
		intQueue = new ArrayList<Ship>();
		intQueue.add(bad);
		intQueue.add(bad2);
		
	}//
	
	
//*************************Combat Turn Order Methods ****************
	
	//use this method to run beginning of turn setup tasks
	// initiative, bookkeeping steps
	public void startTurn(){
		currentAction = 0;
		setInitiative();
		
		System.out.println("start");
		nextAction();
	}//end startTurn method
	
	
	//order actions and such
	public void nextAction(){
	
		
		while (currentAction < (intQueue.size()) && !isPlayer(intQueue.get(currentAction))){
			enemyTurn(intQueue.get(currentAction));
		}//end for
			
		System.out.println("player turn:"+ currentAction);
		
		
	}//end mainTurn
	
	
	
	
	
	public void enemyTurn(Ship s){
		
		//TODO make this work
		//create "AI" packages in ship?
		//random number gen for hit and damage methods
		System.out.println("Enemy: " + intQueue.get(currentAction).getName() + " " + currentAction);
	
		currentAction++;
	}//end enemyTurn
	
	
	private void setInitiative(){
		//intQueue.clear();
		
		
		for (int i = 0; i < intQueue.size()-1;i++){
		
			Ship temp;
			
			if (intQueue.get(i).getCurrentInitiative() > intQueue.get(i+1).getCurrentInitiative()){
				temp = intQueue.get(i);
				intQueue.set(i, intQueue.get(i+1));
				intQueue.set(i+1, temp);
				i = 0;
			}//end if
			
			
		}//end for
		
		
		//debug stuff
		for ( int i = 0; i < intQueue.size() - 1; i++){
			System.out.println(intQueue.get(i).getName());
		}//end debug loop
		
		
	}// end getInitiative
	
	
	
	//*************************** COMBAT ACTION METHODS **********************
	
	// Scans the opponent and returns and estimate of their health
	public void combatScan(){
	
		Game.getGUI().addText("Scanning enemy ship...");
		

		scanHull = Math.round((bad.getCurrentHull()/bad.getHull())*100);
		
		System.out.println("Their hull strength is " + scanHull + ".");
	
		if (scanHull == 100){
			Game.getGUI().addText("The ship appears to be undamaged!");
		}//if for full hull
		
		if (scanHull < 100 && scanHull > 60){
			Game.getGUI().addText("The ship appears lightly damaged.");
		}// if for above 2/3 hull
	
		if (scanHull <= 60 && scanHull > 30){
			Game.getGUI().addText("The ship appears to be moderatly damaged");
		}//if for 60-30% hull
	
		if (scanHull <= 30 && scanHull > 0){
			Game.getGUI().addText("The ship appear to be severly damaged");
		}//if for less than 30%
		
	}//end combatScan
	
	
	
	public void combatAttack(){
		Game.getGUI().addText("Firing Weapons!");
		currentAction++;
		nextAction();
//TODO add damange function		
		
	}//end combatAttack
	
	
	public static void combatEvade(){
		Game.getGUI().addText("Evading");
//TODO add evasion function		
		
	}//end combatEvade
	
	
// ****************************** COMBAT CHECKS AND CLEAUP **********************	
	
	public void stillAlive(){
	
		
// TODO  Need to look at the combat logic. I don't think this will work		
		if(Game.getPlayer().getShip().getCurrentHull() <= 0){
			combatActive = false;
		}//end player if
		
		if(bad.getCurrentHull() <= 0){
			combatActive = false;
		}//end bad if
		
		
	}//end stillAlive
	
	
	
	private boolean isPlayer(Ship s){
		boolean isEqual = false;
		
		if(Game.getPlayer().getShip().equals(s)){
			isEqual = true;
		}//end if
		
		
		return isEqual;
	}//end isPlayer
	
	
	
	
	
	
}//fin
